define([],function(){
    function pageLogic(config){
      this.pageview = config.pageview;
      this.getUserInfo();
    }
    pageLogic.prototype = {
      header_logo_click: function(sender,params){
          var spaceUrl = "http://upesn.com/space/home/index/VISITID/"+this.pageview.pageManager.userInfo.sz_id;
          window.location.href = spaceUrl;
      },
      _loadSuccess:function(data){
          var _this = this;

        this.pageview.pageManager.userInfo = data.data;
        var headImgUrl = data.data.head_img_url ? data.data.head_img_url : "./imgs/header.png";
        this.pageview.delegate("userHeader",function(target){
          target.setSrc(headImgUrl);
        });

        this.pageview.delegate("space_name",function(target){
          target.setText(data.data.szName);
        });

        this.is_admin = data.data.is_admin===1;
        this.dept_manager = data.data.dept_manager!==0;
        this.group_manager = data.data.group_manager===true;
        this.pageview.delegate("main_left_menu",function(target){
            if(_this.is_admin){
              target.addMenu([{"icon": "icomoon_e921", "text": "模板管理", "children": [], "href": "index/template"}]);
            }

            for(var i=0,j=target.items.length;i<j;i++){
                var menuText = target.items[i].$el.text();
                if(!_this.dept_manager){
                    if(menuText==="下属日志"){
                        target.items[i].$el.hide();
                    }
                }
                if((!_this.is_admin&&!_this.dept_manager&&!_this.group_manager)){
                    if(menuText==="日志统计"){
                        target.items[i].$el.hide();
                    }
                    if(menuText==="按部门统计"){
                        target.items[i].$el.hide();
                    }
                    if(target.items[i].$el.text()==="按团队统计"){
                        target.items[i].$el.hide();
                    }
                }else{
                    if(!_this.dept_manager&&!_this.is_admin){
                        if(menuText==="按部门统计"){
                            target.items[i].$el.hide();
                        }

                    }
                    if(!_this.group_manager&&!_this.is_admin){
                        if(target.items[i].$el.text()==="按团队统计"){
                            target.items[i].$el.hide();
                        }

                    }

                }

            }
        });


        this.pageview.pageManager.trigger("userloaded",{data:data});



        if(data.data.showFeatures){

          var featureStr = this.pageview.pageManager.appConfig.feature;
          if(featureStr&&featureStr.length>0){
            this.pageview.delegate("feature_dialog_content",function(target){
              target.setText(featureStr);
            });
            this.pageview.showDialog("feature_dialog");
          }
        }

      },

      feature_dialog_ok_click:function(){
        this.pageview.hideDialog("feature_dialog");
      },


      getUserInfo:function(){
        var _this = this;
        this.pageview.showSubmiting({
          text:"正在加载..",
          successText:null
        });
        this.pageview.ajax({
          url:"/main/getUserInfo",
          type:"GET",
          timeout:8000,
          data:{
            version:this.pageview.pageManager.appConfig.version
          },
          success:function(data){
            if(data.code===0){
              _this._loadSuccess(data);
            }else{
              _this.pageview.showTip({"text":"获取人员信息失败！请刷新页面再试"});
            }
            _this.pageview.hideSubmiting(true);
          },
          error:function(error){
            _this.pageview.showTip({"text":"获取人员信息失败！请刷新页面再试"});
            _this.pageview.hideSubmiting(true);
          }
        });
      }

    };
    return pageLogic;
});
